package com.example.javasystem.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.javasystem.entity.Account;
import com.example.javasystem.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 从数据库查询用户（假设Account实体有username和password字段）
        Account account = accountMapper.selectOne(
                new QueryWrapper<Account>().eq("username", username)
        );
        if (account == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        // 返回Spring Security需要的用户信息（用户名、密码、权限）
        return User.withUsername(account.getUsername())
                .password(account.getPassword())  // 假设密码已加密存储
                .authorities("ROLE_USER")  // 赋予默认角色
                .build();
    }
}